Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): HandlerFunction argument #464

Merged
merged 2 commits into from
Mar 3, 2021
Merged

Conversation

jablko
Copy link
Contributor

@jablko jablko commented Feb 13, 2021

What do you think about revising HandlerFunction as follows, to make the argument TTransformed if the transform option is set and EmitterWebhookEvent<TName> if not, instead of always the intersection?

--- a/src/types.ts
+++ b/src/types.ts
@@ -32,8 +32,8 @@ type TransformMethod<T> = (event: EmitterWebhookEvent) => T | PromiseLike<T>;
 
 export type HandlerFunction<
   TName extends EmitterWebhookEventName,
-  TTransformed
-> = (event: EmitterWebhookEvent<TName> & TTransformed) => any;
+  TTransform
+> = (event: TTransform extends TransformMethod<infer T> ? T : EmitterWebhookEvent<TName>) => any;
 
 type Hooks = {
   [key: string]: Function[];

This fixes the types in cases like:

--- a/test/integration/event-handler-test.ts
+++ b/test/integration/event-handler-test.ts
@@ -131,15 +131,15 @@ test("options.transform", (done) => {
   const eventHandler = createEventHandler({
     transform: (event) => {
       expect(event.id).toBe("123");
       return "funky";
     },
   });
 
-  eventHandler.on("push", (event: EmitterWebhookEvent) => {
+  eventHandler.on("push", (event: string) => {
     expect(event).toBe("funky");
 
     done();

G-Rath
G-Rath previously approved these changes Feb 15, 2021
Copy link
Member

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever! I clearly need to step up my conditional typing game 😉

@gr2m gr2m added Type: Bug Something isn't working as documented, or is being fixed typescript Relevant to TypeScript users only labels Feb 15, 2021
Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍🏼

@wolfy1339 wolfy1339 requested a review from G-Rath March 3, 2021 19:00
@wolfy1339
Copy link
Member

@G-Rath can you do a final review and merge this

@G-Rath G-Rath merged commit aad9b74 into octokit:master Mar 3, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2021

🎉 This PR is included in version 8.5.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

oscard0m added a commit that referenced this pull request Mar 14, 2021
oscard0m added a commit that referenced this pull request Mar 14, 2021
oscard0m added a commit that referenced this pull request Mar 14, 2021
oscard0m added a commit that referenced this pull request Mar 14, 2021
* reverts: "fix(typescript): HandlerFunction argument (#464)"

This reverts commit aad9b74.

* reverts: "fix(typescript): infer `TTransformed` from `createEventHandler()` options (#459)"

This reverts commit d2a0b73.

* fix(types): adopt options type from first revert

* test(typescript-validate): added explanatory comment for ts validation test
This was referenced Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working as documented, or is being fixed typescript Relevant to TypeScript users only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants